//
//  CarConfigChooseVC.m
//  New_Runway
//
//  Created by mack on 2018/1/31.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarConfigChooseVC.h"
#import "CarConfigHeaferView.h"
#import "CarConfigCell.h"
#import "CarBaseInfoCell.h"

#import "GetCarConfModel.h"

@interface CarConfigChooseVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;

@property (strong, nonatomic) NSMutableArray    *selectIndexs;  //多选选中的行

@end

@implementation CarConfigChooseVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"车辆配置";
    self.view.backgroundColor = kColorWhite();
    [self.navigationBarView.rightItem setTitle:@"确定" forState:UIControlStateNormal];
    [self initTableview];
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[CarConfigHeaferView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableview registerClass:[CarConfigCell class] forCellReuseIdentifier:@"CarConfigCell"];
    
    [self.tableview registerClass:[CarBaseInfoCell class] forCellReuseIdentifier:@"CarBaseInfoCell"];
    
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_offset(-self.BotH);

    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArray.count ;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    confResult *info = self.listArray[section];
    
    if (info.selectedState == NO) {
        return 0;
    }else{
        
        return info.childCarConf.count;
    }
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 41;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    confResult *info = self.listArray[indexPath.section];
    Childcarconfig *childInfo = info.childCarConf[indexPath.row];
    CarConfigCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarConfigCell"];
    cell.label.text = childInfo.confName;
//    cell.leftImage.image = [UIImage imageNamed:@"circle"];
//    childInfo.selectedState = NO;
    if (childInfo.selectedState == YES) {
        cell.leftImage.image = [UIImage imageNamed:@"hook"];
        
        
    }else{
        
        cell.leftImage.image = [UIImage imageNamed:@"circle"];
        
    }
    for (NSIndexPath *index in _selectIndexs) {
        if (indexPath == index) {
            childInfo.selectedState = YES;
            cell.leftImage.image = [UIImage imageNamed:@"hook"];
        }
    }
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    confResult *info = self.listArray[section];
    
    CarConfigHeaferView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    view.label.text = info.confName;
    view.tag = section;
    if (info.selectedState == YES) {
        view.rightImage.image = [UIImage imageNamed:@"up"];
        
    }else{
        view.rightImage.image = [UIImage imageNamed:@"down"];
    }
    
    
    if (view.gestureRecognizers == nil) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerViewClickedAction:)];
        [view addGestureRecognizer:tap];
    }
    
    return view;
    
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    confResult *info = self.listArray[indexPath.section];
    Childcarconfig *childInfo = info.childCarConf[indexPath.row];
    
    CarConfigCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (childInfo.selectedState == YES) { //如果为选中状态
        childInfo.selectedState = NO;
        cell.leftImage.image =[UIImage imageNamed:@"circle"];
        //切换为未选中
        [_selectIndexs removeObject:indexPath]; //数据移除
        
        [self.configIds removeObject:[NSString stringWithFormat:@"%zd",childInfo.confId]];
        [self.configNames removeObject:childInfo.confName];

    }else { //未选中
        childInfo.selectedState = YES;
        cell.leftImage.image = [UIImage imageNamed:@"hook"];
        [self.configIds addObject:[NSString stringWithFormat:@"%zd",childInfo.confId]];
        [self.configNames addObject:childInfo.confName];
        [_selectIndexs addObject:indexPath]; //添加索引数据到数组
    }
    
    
    
    
}
- (void) headerViewClickedAction:(UITapGestureRecognizer *)sender
{
    
    confResult *info = self.listArray[sender.view.tag];
    
    
    if (info.selectedState == YES) {
        info.selectedState = NO;
        
    }else{
        info.selectedState = YES;
        
    }
    
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:sender.view.tag];
    [self.tableview reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 41;
}

-(void)rightItemClick{
    [self.navigationController popViewControllerAnimated:YES];
    self.configBlock(self.configIds, self.configNames);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
